### This script will average each BRT daily prediction with the 6 previous days
### Resulting ASCII written out for summary to week-based microCLIM

# Obtain command line arguments from .sh file

args=(commandArgs(TRUE))

# Evaluate the arguments for use in this script

for(i in 1:length(args)) 
	
	{
		
	eval(parse(text=args[[i]]))
 
	}

### Load library

library('SDMTools')

### Establish out directory

out.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/brtpreds7daymovavg/'

### List directories where BRT preds are

brt.max.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/brtpredsfinal/maxgzip/'
brt.min.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/brtpredsfinal/mingzip/'

### List all BRT files

brt.max.files = list.files(brt.max.dir, full.names=T, recursive=T, pattern='.asc.gz')
brt.min.files = list.files(brt.min.dir, full.names=T, recursive=T, pattern='.asc.gz')

### Convert argument yearx from string to numeric

yearx = as.numeric(yearx)

### Identify all files in yearx for max and min preds
	
max.fois = brt.max.files[grep(yearx,brt.max.files)]
min.fois = brt.min.files[grep(yearx,brt.min.files)]
	
### Need to add the last six files from the previous year as well
	
max.6 = brt.max.files[grep(yearx-1,brt.max.files)][360:365]
min.6 = brt.min.files[grep(yearx-1,brt.min.files)][360:365]
	
### Need to add the first file from the following year as well
	
max.1 = brt.max.files[grep(yearx+1,brt.max.files)][1]
min.1 = brt.min.files[grep(yearx+1,brt.min.files)][1]
	
### Concatenate lists
	
max.fois = c(max.6,max.fois,max.1)
min.fois = c(min.6,min.fois,min.1)

### Read in an ASCII template

base.asc = read.asc.gz(max.fois[1])

### Get some base pos

base.pos = as.data.frame(which(is.finite(base.asc), arr.ind = T))
base.pos$long = getXYcoords(base.asc)$x[base.pos$row]
base.pos$lat = getXYcoords(base.asc)$y[base.pos$col]

### Use a for loop

### Setting up the moving window matrix by reading in the first seven fois for max and min

for (i in 1:7)
	
	{
	
	### Read in the appropriate max and min files
	
	maxasc = read.asc.gz(max.fois[i])
	minasc = read.asc.gz(min.fois[i])
	
	### Bind data from temp ASCII's onto base.pos
		
	base.pos = cbind(base.pos,extract.data(cbind(base.pos$long,base.pos$lat),maxasc))
	base.pos = cbind(base.pos,extract.data(cbind(base.pos$long,base.pos$lat),minasc))
		
	cat(i,'\n')
		
	}
	
### Close matrix setup loop

for (i in 8:length(max.fois))

	{
	
	### Calculate the moving average for that day	
		
	base.pos[,19]=rowMeans(base.pos[,c(5,7,9,11,13,15,17)])
	base.pos[,20]=rowMeans(base.pos[,c(6,8,10,12,14,16,18)])

	### Bind max data onto base.asc and write out
	
	base.asc[cbind(base.pos$row,base.pos$col)]=base.pos[,19]
	write.asc.gz(base.asc,file=paste(out.dir,'maxgzip/',yearx,'/',gsub('.asc.gz','',basename(max.fois[i-1])),sep=''))

	### Bind min data onto base.asc and write out

	base.asc[cbind(base.pos$row,base.pos$col)]=base.pos[,20]
	write.asc.gz(base.asc,file=paste(out.dir,'mingzip/',yearx,'/',gsub('.asc.gz','',basename(min.fois[i-1])),sep=''))
	
		### Don't read in last file, not really necessary
		
		if(i==length(max.fois))
	
		{
	
		break
	
		}
	
	### Read in the ith fois files
	
	maxasc = read.asc.gz(max.fois[i])
	minasc = read.asc.gz(min.fois[i])
	
	### Remove columns 5, 6, 19, and 20
	
	base.pos = base.pos[,-c(5,6,19,20)]
	
	### Bind data from temp ASCII's onto base.pos
		
	base.pos = cbind(base.pos,extract.data(cbind(base.pos$long,base.pos$lat),maxasc))
	base.pos = cbind(base.pos,extract.data(cbind(base.pos$long,base.pos$lat),minasc))
	
	# Report progress
	
	cat(i,'\n')
	
	}

# Done

